package LifeGame11;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Time;
import java.util.Scanner;

public class Frame extends JFrame implements ActionListener {
    private static Frame frame;
    private Cell cell;
    private int interval;
    public int fLength,fWidth;
    private JButton[][] nGrid;
    private boolean[][]isSelected;
    private JButton jbNowGeneration,randomInit,clearGeneration;
    private JButton clearCell,nextGeneration,start,stop,exit;
    private JComboBox lengthList,widthList;
    private  boolean isRunning;
    private Thread thread;
    private  boolean isDead;
    public void setMaxWidth(int maxWidth) {
        this.fWidth = maxWidth;
    }

    public int getInterval(){
       return interval;
    }
    //public void setMaxLength(int maxLength) {
       // this.fLength = maxLength;
    //}
    @SuppressFBWarnings("DM_EXIT")
    public void initGUI(int maxWidth, int maxLength, Timer timer) {
        interval = timer.getInterval();
        fWidth = maxWidth;
        fLength = maxLength;

        if (fWidth == 0||fLength==0) {
            System.out.println("界面设计出错");
            System.exit(0);
        }
        cell = new Cell(fWidth,fLength);
        JPanel backPanel,centerPanel,bottomPanel;
        JLabel jWidth,jLength,jNowGeneration;
        backPanel = new JPanel(new BorderLayout());
        centerPanel = new JPanel(new GridLayout(fWidth,fLength));
        bottomPanel = new JPanel();
        this.setContentPane(backPanel);
        backPanel.add(centerPanel,"Center");
        backPanel.add(bottomPanel,"North");
        nGrid = new JButton[fWidth][fLength];
        isSelected = new boolean[fWidth][fLength];
        for(int i=0;i<fWidth;i++) {
            for(int j =0;j<fLength;j++) {
                nGrid[i][j] = new JButton("");
                nGrid[i][j].setBackground(Color.white);
                centerPanel.add(nGrid[i][j]);
            }
        }

        clearCell = new JButton("清空画质");
        randomInit = new JButton("随机生成新画面");
        //clearGeneration = new JButton("代数清零");
        start = new JButton("开始不断繁衍");
        nextGeneration = new JButton("进行逐代演化");
        stop = new JButton("结束不断演化");
        exit = new JButton("退出界面");
        bottomPanel.add(randomInit);
        bottomPanel.add(clearCell);
        bottomPanel.add(start);
        bottomPanel.add(nextGeneration);
        bottomPanel.add(stop);
        bottomPanel.add(exit);
        this.setSize(1000,650);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.addWindowListener(new WindowAdapter() {
            public void windowClosed(WindowEvent e) {
                System.exit(0);
            }
        });
        randomInit.addActionListener(this);
        clearCell.addActionListener(this);
        nextGeneration.addActionListener(this);
        start.addActionListener(this);
        stop.addActionListener(this);
        exit.addActionListener(this);
        for (int i = 0; i < maxWidth; i++) {
            for (int j = 0; j < maxLength; j++) {
                nGrid[i][j].addActionListener(this);
            }
        }
    }
    @SuppressFBWarnings({"ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", "DM_EXIT"})
    public void actionPerformed(ActionEvent e) {
        frame = new Frame("生命游戏");
        if(e.getSource()==randomInit) {//随机初始化
            cell.randomCell();
            showCell();
            isRunning = false;
            thread = null;
        }else if(e.getSource()==clearCell) {
            //细胞清零
            cell.deleteAllCell();
            showCell();
            isRunning =false;
            thread = null;
        }else if(e.getSource()==start) {
            //开始游戏
            isRunning = true;
            thread  = new Thread(new Runnable() {
                public void run() {
                    while (isRunning) {
                        makeNextGeneration();
                        try {
                            Thread.sleep(interval);
                        }catch(InterruptedException e1) {
                            e1.printStackTrace();
                        }
                      isDead();
                    }
                }
            });
            thread.start();
        }else if(e.getSource()==nextGeneration) {//下一代
            makeNextGeneration();
            isDead();
            isRunning  = false;
            thread = null;
        }else if(e.getSource()==exit) {//退出
            frame.dispose();
            System.exit(0);
        }else if(e.getSource()==stop) {
            //暂停
            isRunning = false;
            thread = null;
        }else {
            int[][] grid = cell.getGrid();
            for(int i =0;i<fWidth;i++) {
                for(int j=0;j<fLength;j++) {
                    if(e.getSource()==nGrid[i][j]) {
                        isSelected[i][j] = !isSelected[i][j];
                    }
                    if(isSelected[i][j]) {
                        nGrid[i][j].setBackground(Color.black);
                        grid[i+1][j+1] =1;
                    }else {
                        nGrid[i][j].setBackground(Color.white);
                        grid[i+1][j+1] = 0;
                    }
                }
            }
        }
    }
    public Frame(String name) {
        super(name);
    }
    public  void isDead(){
        isDead = true;
        for(int row = 1;row<=fWidth;row++) {
            for(int col=1;col<=fLength;col++) {
                if(cell.getGrid()[row][col]==1) {
                    isDead = false;
                    break;
                }
            }if(!isDead) {
                break;
            }
        }if(isDead) {
            JOptionPane.showMessageDialog(null, "所有细胞已死亡");
            isRunning = false;
            thread = null;
        }
    }
    public  void makeNextGeneration() {
        cell.update();
        showCell();
       // jbNowGeneration.setText(""+cell.getNowGeneration());
    }
    public  void showCell() {
        int[][] grid = cell.getGrid();
        boolean[][] visited = cell.getIsVisited();
        for(int i = 0;i<fWidth;i++) {
            for(int j = 0;j<fLength;j++) {
                if(grid[i+1][j+1]==1&&visited[i+1][j+1]==true) {
                    nGrid[i][j].setBackground(Color.black);
                }else if(grid[i+1][j+1]==0&&visited[i+1][j+1]==true) {
                    nGrid[i][j].setBackground(Color.white);
                }
            }
        }
    }

}